<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/21
 * Time: 23:23
 */

namespace app\api\controller\v1;


use app\api\validate\Count;
use app\api\Model\Product as ProductModel;
use app\api\validate\IDMustBePostiveInt;
use app\lib\exception\ProductException;

class Product
{
    /**
     * @api {get} /product/all 获取所有产品
     * @apiName getAll
     * @apiGroup Product
     *
     * @apiSuccess {int} id   id
     * @apiSuccess {varchar} title 标题
     * @apiSuccess {varchar} erji_title 二级标题
     * @apiSuccess {varchar} phone 图片
     * @apiSuccess {text} product_introduct 产品详情
     * @apiSuccess {decimal} new_perice 现价
     * @apiSuccess {decimal} old_perice 原价
     * @apiErrorExample {string} Error-Response:
     * 404 : 获取产品不存在
     */
    public function getAll($count=15){
//        (new Count())->goCheck();
        $products = ProductModel::select();

        if($products->isEmpty()){
            throw new ProductException();
        }
//        $collection = collection($products);
//        $products =$collection->hidden(['summary']);
//        $products =$products->hidden(['summary']);
        return $products;
    }


    /**
     * @api {get} /product/by_category/:id 根据分类获取产品
     * @apiName by_category/:id
     * @apiGroup Product
     *
     * @apiParam {Int} id 分类id
     *
     *  @apiSuccess {Int} id 产品id
     * @apiSuccess {varchar} title 标题
     * * @apiSuccess {varchar} erji_title 二级标题
     * @apiSuccess {varchar} phone 图片
     * @apiSuccess {text} product_introduct 产品详情
     * @apiSuccess {decimal} new_perice 现价
     * @apiSuccess {decimal} old_perice 原价
     * @apiErrorExample {string} Error-Response:
     * 404 : 获取产品不存在
     */
    public function getAllInCategory($id){
        (new IDMustBePostiveInt())->goCheck();
        $products = ProductModel::getProductsByCategoryTD($id);
        if($products->isEmpty()){
            throw new ProductException();
        }
        $products =$products->hidden(['summary']);
        return $products;

    }

    /**
     * @api {get} /product/:id 根据id获取产品详情
     * @apiName /product/:id
     * @apiGroup Product
     *
     * @apiParam {Int} id 产品id
     *
     * @apiSuccess {varchar} title 标题
     * * @apiSuccess {varchar} erji_title 二级标题
     * @apiSuccess {varchar} phone 图片
     * @apiSuccess {text} product_introduct 产品详情
     * @apiSuccess {decimal} new_perice 现价
     * @apiSuccess {decimal} old_perice 原价
     * @apiErrorExample {string} Error-Response:
     * 404 : 获取产品不存在
     */
    public function getOne($id)
    {
        (new IDMustBePostiveInt())->goCheck();
        $product = ProductModel::getProductDetail($id);
        if(!$product){
            throw new ProductException();
        }
        return $product;
    }

    /**
     * @api {get} /product/:id 根据id获取产品详情
     * @apiName /product/:id
     * @apiGroup Product
     *
     * @apiParam {Int} id 产品id
     *
     * @apiSuccess {Int} id 产品id
     * @apiSuccess {varchar} title 标题
     * * @apiSuccess {varchar} erji_title 二级标题
     * @apiSuccess {varchar} phone 图片
     * @apiSuccess {text} product_introduct 产品详情
     * @apiSuccess {decimal} new_perice 现价
     * @apiSuccess {decimal} old_perice 原价
     * @apiErrorExample {string} Error-Response:
     * 404 : 获取产品不存在
     */
    public function getProductCateAll($id)
    {
        (new IDMustBePostiveInt())->goCheck();
        $product = ProductModel::getProductCate($id);
        if(!$product){
            throw new ProductException();
        }
        return $product;
    }


    /**
     * @api {get} /product/getProductReAll 获取热销产品
     * @apiGroup Product
     *
     *@apiSuccess {Int} id 产品id
     * @apiSuccess {varchar} title 标题
     * * @apiSuccess {varchar} erji_title 二级标题
     * @apiSuccess {varchar} phone 图片
     * @apiSuccess {text} product_introduct 产品详情
     * @apiSuccess {decimal} new_perice 现价
     * @apiSuccess {decimal} old_perice 原价
     * @apiErrorExample {string} Error-Response:
     * 404 : 获取产品不存在
     */
    public function getProductReAll()
    {
        $products = ProductModel::where('status','1')->select();
        if($products->isEmpty()){
            throw new ProductException();
        }
        return $products;
    }

    /**
     * @api {get} /product/highPerice  产品价格从高到低排序
     * @apiName /highPerice
     * @apiGroup Product
     *
     *@apiSuccess {Int} id 产品id
     * @apiSuccess {varchar} title 标题
     * * @apiSuccess {varchar} erji_title 二级标题
     * @apiSuccess {varchar} phone 图片
     * @apiSuccess {text} product_introduct 产品详情
     * @apiSuccess {decimal} new_perice 现价
     * @apiSuccess {decimal} old_perice 原价
     * @apiErrorExample {string} Error-Response:
     * 404 : 获取产品不存在
     */

    public function highPerice(){
        $products = ProductModel::order('new_perice desc')->select();
        if(!$products){
            throw new ProductException();
        }
        return $products;
    }
    /**
     * @api {get} /product/lowPerice  产品价格从低到高排序
     * @apiName /lowPerice
     * @apiGroup Product
     *
     *@apiSuccess {Int} id 产品id
     * @apiSuccess {varchar} title 标题
     * * @apiSuccess {varchar} erji_title 二级标题
     * @apiSuccess {varchar} phone 图片
     * @apiSuccess {text} product_introduct 产品详情
     * @apiSuccess {decimal} new_perice 现价
     * @apiSuccess {decimal} old_perice 原价
     * @apiErrorExample {string} Error-Response:
     * 404 : 获取产品不存在
     */
    public function lowPerice(){
        $products = ProductModel::order('new_perice asc')->select();
        if(!$products){
            throw new ProductException();
        }
        return $products;
    }

    /**
     * @api {get} /product/highNum  产品销量从高到低排序
     * @apiName /highNum
     * @apiGroup Product
     *
     *@apiSuccess {Int} id 产品id
     * @apiSuccess {varchar} title 标题
     * * @apiSuccess {varchar} erji_title 二级标题
     * @apiSuccess {varchar} phone 图片
     * @apiSuccess {text} product_introduct 产品详情
     * @apiSuccess {decimal} new_perice 现价
     * @apiSuccess {decimal} old_perice 原价
     * @apiErrorExample {string} Error-Response:
     * 404 : 获取产品不存在
     */

    public function highNum(){
        $products = ProductModel::order('sales_num desc')->select();
        if(!$products){
            throw new ProductException();
        }
        return $products;
    }
    /**
     * @api {get} /product/lowNum  产品销量从低到高排序
     * @apiName /lowNum
     * @apiGroup Product
     *
     *@apiSuccess {Int} id 产品id
     * @apiSuccess {varchar} title 标题
     * * @apiSuccess {varchar} erji_title 二级标题
     * @apiSuccess {varchar} phone 图片
     * @apiSuccess {text} product_introduct 产品详情
     * @apiSuccess {decimal} new_perice 现价
     * @apiSuccess {decimal} old_perice 原价
     * @apiErrorExample {string} Error-Response:
     * 404 : 获取产品不存在
     */
    public function lowNum(){
        $products = ProductModel::order('sales_num asc')->select();
        if(!$products){
            throw new ProductException();
        }
        return $products;
    }

    /**
     * @api {post} /product/searchProduct  根据标题搜索产品
     * @apiName /searchProduct
     * @apiGroup Product
     *
     * @apiParam {varchar} title 标题
     *
     * @apiSuccess {Int} id 产品id
     * @apiSuccess {varchar} title 标题
     * * @apiSuccess {varchar} erji_title 二级标题
     * @apiSuccess {varchar} phone 图片
     * @apiSuccess {text} product_introduct 产品详情
     * @apiSuccess {decimal} new_perice 现价
     * @apiSuccess {decimal} old_perice 原价
     * @apiErrorExample {string} Error-Response:
     * 404 : 获取产品不存在
     */

    public function searchProduct()
    {
        if (request()->isPost()) {
            $title = input('post.title');
            $map['title'] = ['like',"%" . $title . "%"];
            $products = ProductModel::where($map)->select();
            if(!$products){
                throw new ProductException();
            }
            return $products;

        }
    }
}